package com.example.controller;

import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {

    /**
     * Controller -> Service -> Mapper 请求数据
     * Mapper -> Service -> Controller 返回cls数据
     */

    //可以用@Slf4j注解代替
    //private static Logger log = LoggerFactory.getLogger(RestController.class);
    @Autowired
    private DeptService deptService;
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)//指定请求方式为GET
    @GetMapping("/depts")
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    @DeleteMapping("/depts/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除ID为"+id+"的部门");
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/depts")
    public Result addDeptName(@RequestBody Dept dept){
        log.info("添加部门{}",dept);
        deptService.addDeptName(dept);
        return Result.success();
    }
}
